
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Text;

namespace Friendster.Service
{
    /// <summary>
    /// Represents the widget class.
    /// </summary>
    public sealed class Widget : FriendsterEntity
    {
        // Fields
        private bool _lastUpdatedTransformed;
        private bool _createdDateTransformed;

        // Methods
        internal Widget(Hashtable widgetState): base(widgetState)
        {
        }
        private DateTime TransformCreatedDate()
        {
            DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string seconds = (string)(base.GetState<string>("created"));
            time = time.AddSeconds(Convert.ToDouble(seconds));
            base["created"] = time;
            this._createdDateTransformed = true;
            return time;
        }
        private DateTime TransformLastUpdatedDate()
        {
            DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string seconds = (string)(base.GetState<string>("lastupd"));
            time = time.AddSeconds(Convert.ToDouble(seconds));
            base["lastupd"] = time;
            this._lastUpdatedTransformed = true;
            return time;
        }

        // Properties
        /// <summary>
        /// Returns the user ID of widget.
        /// </summary>
        public string UserID
        {
            get
            {
                return (base.GetState<string>("uid") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns the instance ID of the widget.
        /// </summary>
        public string InstanceID
        {
            get
            {
                return (base.GetState<string>("instance_id") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns the title data of widget.
        /// </summary>
        public string Title
        {
            get
            {
                return (base.GetState<string>("title") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns the widget content.
        /// </summary>
        public string Content
        {
            get
            {
                return (base.GetState<string>("content") ?? string.Empty);
            }
        }
        /// <summary>
        /// Returns the datetime object of when the widget has been created.
        /// </summary>
        public DateTime CreatedDate
        {
            get
            {
                if (!this._createdDateTransformed)
                    return this.TransformCreatedDate();

                return (DateTime)base["created"];
            }
        }
        /// <summary>
        /// Returns the datetime object of when the widget has been last updated.
        /// </summary>
        public DateTime LastUpdatedDate
        {
            get
            {
                if (!this._lastUpdatedTransformed)
                    return this.TransformLastUpdatedDate();

                return (DateTime)base["lastupd"];
            }
        }
    }
}
